package com.cskaoyan.servlet;


import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.common.CommonServlet;
import com.cskaoyan.model.MarketAdmin;
import com.cskaoyan.service.AdminAuthService;
import com.cskaoyan.service.impl.AdminAuthServiceImpl;
import com.cskaoyan.util.JacksonUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/admin/auth/*")
public class AdminAuthServlet extends CommonServlet {
    private static final AdminAuthService ADMIN_AUTH_SERVICE=new AdminAuthServiceImpl();
    public static final String ADMIN_KEY = "market_user_id";



    // login
    public BaseRespVo login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String jsonStr = request.getReader().readLine();
        Map<String,String> map = JacksonUtil.read(jsonStr, Map.class);
        String username = map.get("username");
        String password = map.get("password");
        MarketAdmin admin = ADMIN_AUTH_SERVICE.findByUsername(username);
        if (password.equals(admin.getPassword())) {
            HttpSession session = request.getSession();
            session.setAttribute(ADMIN_KEY,admin.getId());
            Map<String,String> adminInfoMap=new HashMap<>();
            adminInfoMap.put("avatar",admin.getAvatar());
            adminInfoMap.put("nickName", admin.getUsername());
            Map<String,Object>dataMap=new HashMap<>();
            dataMap.put("adminInfo",adminInfoMap);
            dataMap.put("token",session.getId());
            return BaseRespVo.ok(dataMap); // CommonServlet中的method.invoke方法的返回值
        } else {
            return BaseRespVo.fail("用户账号或密码不正确",605);
        }

    }

    // logout
    public BaseRespVo logout(HttpServletRequest request, HttpServletResponse response) {
        request.getSession().invalidate();
        return BaseRespVo.msg("已注销");
    }
    // info
    public BaseRespVo info(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        // 在Session中拿到再/admin/auth/login这里登录成功的时候存入的信息
        // 然后根据adminId做对应的查询
        Integer adminId = (Integer) session.getAttribute(ADMIN_KEY);
        MarketAdmin admin = ADMIN_AUTH_SERVICE.findAdminById(adminId);
        if (admin!=null){
            Map<String,Object> data = new HashMap<>();
            data.put("avatar", admin.getAvatar());
            data.put("nickName", admin.getUsername());
            data.put("perms", Arrays.asList("*"));
            // String roleIdsStr = admin.getRoleIds();
            // Integer[]  roleIds = JacksonUtil.read(roleIdsStr, Integer[].class);
            data.put("roles", admin.getRoleIds());
            return BaseRespVo.ok(data);
        }else {
            return BaseRespVo.fail("获取用户信息失败",605);
        }



    }
    // modify
    public BaseRespVo modify(HttpServletRequest request, HttpServletResponse response) {
        // 写修改业务代码
        return BaseRespVo.msg("修改成功");
    }
    // remove
    public BaseRespVo remove(HttpServletRequest request, HttpServletResponse response) {
        // 写删除业务代码
        return BaseRespVo.msg("删除成功");
    }
    public void testFi(){
        System.out.println("USER_KEY = ");
    }

}
